package org.dromara.business.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import cn.hutool.core.util.StrUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.dromara.business.domain.bo.LotteryRecordsBo;
import org.dromara.business.domain.vo.LotteryRecordsVo;
import org.dromara.business.domain.LotteryRecords;
import org.dromara.business.mapper.LotteryRecordsMapper;
import org.dromara.business.service.LotteryRecordsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 抽奖记录Service业务层处理
 *
 * @author Lion Li
 * @date 2025-01-13
 */
@RequiredArgsConstructor
@Service
public class LotteryRecordsServiceImpl extends ServiceImpl<LotteryRecordsMapper,LotteryRecords> implements LotteryRecordsService {

    private final LotteryRecordsMapper baseMapper;

    /**
     * 查询抽奖记录
     */
    @Override
    public LotteryRecordsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询抽奖记录列表
     */
    @Override
    public TableDataInfo<LotteryRecordsVo> queryPageList(LotteryRecordsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LotteryRecords> lqw = buildQueryWrapper(bo);
        Page<LotteryRecordsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询抽奖记录列表
     */
    @Override
    public List<LotteryRecordsVo> queryList(LotteryRecordsBo bo) {
        LambdaQueryWrapper<LotteryRecords> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LotteryRecords> buildQueryWrapper(LotteryRecordsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LotteryRecords> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getAward()), LotteryRecords::getAward, bo.getAward());
        lqw.eq(bo.getCreateBy() != null, LotteryRecords::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, LotteryRecords::getCreateTime, bo.getCreateTime());
        lqw.orderByDesc(LotteryRecords::getCreateTime);
        return lqw;
    }

    /**
     * 新增抽奖记录
     */
    @Override
    public Boolean insertByBo(LotteryRecordsBo bo) {
        LotteryRecords add = MapstructUtils.convert(bo, LotteryRecords.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改抽奖记录
     */
    @Override
    public Boolean updateByBo(LotteryRecordsBo bo) {
        LotteryRecords update = MapstructUtils.convert(bo, LotteryRecords.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LotteryRecords entity){

    }

    /**
     * 批量删除抽奖记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){

        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
